// pages/recruitPersonalInfo/recruitPersonalInfo.js
var app = getApp();
const util = require('../../utils/util.js');
var model = require('../../utils/model/model.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    formlist: [
      // { isrequire: false, label: '家乡', key: 'homeTown', valueType: 'text', tips: '请选择', type: 'select', mode: 'region' },
      { isrequire: true, label: '姓名', key: 'name', valueType: 'text', tips: '请填写', type: 'input'},
      { isrequire: true, label: '性别', key: 'sex', valueType: 'text', tips: '请选择', type: 'select', data: ['男','女']},
      { isrequire: true, label: '出生年月', key: 'birthday', valueType: 'text', tips: '请选择', type: 'select', mode: 'date', fields: 'day'},
      { isrequire: true, label: '参加工作时间', key: 'enterWorkDate', valueType: 'text', tips: '请选择', type: 'select', mode: 'date', fields:'month' },
      { isrequire: true, label: '现居住城市', key: 'currentCity', valueType: 'text', tips: '请选择', type: 'select', mode:'region'},
      { isrequire: true, label: '户口所在地', key: 'areaOfHukou', valueType: 'text', tips: '请选择', type: 'select', mode: 'region'},
      { isrequire: true, label: '学历', key: 'educational', valueType: 'text', tips: '请选择', type: 'select', data: ['博士后', '博士 ', '硕士', '本科', '专科', '高中', '高中以下'] },
      { isrequire: true, label: '手机号(已绑定)', key: 'telephone', valueType: 'number', tips: '请填写', type: 'input', reg: "^1[34578][0-9]{9}$" },
      { isrequire: true, label: '邮箱地址', key: 'mailAddress', valueType: 'text', tips: '请填写', type: 'input', reg: "^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$"}
    ],
    formData: {
      name: '',
      sex: "",
      birthday: '',
      enterWorkDate: '',
      currentCity: '',
      areaOfHukou: '',
      educational: '',
      telephone: '',
      mailAddress: ''
    },
    canSubmit: false,
    modelData: {
      showModal: false,
      signle: true,
      signleBtnTxt: "确定",
      content: '',
    },
  },
  bindPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.data) {
      this.data.formData[item.key] = item.data[e.detail.value]
    } else {
      this.data.formData[item.key] = e.detail.value
    }
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    model.init.apply(this, ['modelData']);
    var now = util.getNow();
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      ['formlist[2].end']: now,
      ['formlist[3].end']: now.substring(0,7)
    })
    this.getUserIdentity();
  },
  getUserIdentity: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/getOne', //仅为示例，并非真实的接口地址
      data: { userId: this.data.userInfo.id },
      success: function (res) {
        if (res.data.success) {
          if (res.data.data) {
            for (var j in that.data.formData) {
              if (res.data.data[j] !== '' && res.data.data[j] != undefined && res.data.data[j] != null) {
                if (j == 'currentCity' || j == 'areaOfHukou') {
                  that.data.formData[j] = res.data.data[j].split(',');
                } else {
                  that.data.formData[j] = res.data.data[j];
                }
              }
            }
            if (that.data.formData.telephone==''){
              that.data.formData.telephone = that.data.userInfo.phone;
            }
            that.data.formData.id = res.data.data.id;
            that.setData({
              formData: that.data.formData
            })
            that.setSelect('sex',1);
            that.setSelect('educational', 6);
            that.changeValue();
          }
        }
      }
    })
  },
  setSelect: function (key, index) {
    if (this.data.formData[key] !== '') {
      var that=this;
      var item = this.data.formlist[index].data;
      item.forEach(function(ele,j){
        if (that.data.formData[key]==ele){
          that.setData({
            ['formlist[' + index + '].index']: j
          })
        }
      })
    }
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    if (key =='mailAddress'){
      this.data.formData[key] = e.detail.value.replace(/\s+/g, "");
    }else{
      this.data.formData[key] = e.detail.value;
    }
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  checkReg: function (data) {
    var flag = true;
    for (var i = 0; i < data.length; i++) {
      if (data[i].reg) {
        var reg = new RegExp(data[i].reg);
        if (!reg.test(this.data.formData[data[i].key])) {
          flag = false;
          this.setData({
            'modelData.showModal': true,
            'modelData.content': '请输入正确的' + data[i].label
          });
          return;
        }
      }
    }
    return flag;
  },
  saveBasicInfo: function (e) {
    if (this.data.canSubmit){
      if (this.checkReg(this.data.formlist) == true) {
        var param = {};
        for (var j in this.data.formData) {
          param[j] = this.data.formData[j];
        }
        if (param.currentCity != '') {
          if (param.currentCity.indexOf(',') < 0) {
            param.currentCity = param.currentCity.join(',');
          }
        }
        if (param.areaOfHukou != '') {
          if (param.areaOfHukou.indexOf(',') < 0) {
            param.areaOfHukou = param.areaOfHukou.join(',');
          }
        }
        var that = this;
        wx.request({
          url: app.globalData.httpApiCar + 'recruitPersonalInfo/update', //仅为示例，并非真实的接口地址
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          data: param,
          success: function (res) {
            if (res.data.success) {
              wx.navigateBack({
                delta: 1
              })
              // wx.redirectTo({
              //   url: '/pages/recruitMine/recruitMine',
              // })
            }
            else {
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    }
  },
  saveCanel: function (e) {
    wx.redirectTo({
      url: '/pages/blindMine/blindMine'
    })
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.name == '') || (item.sex == '') || (item.birthday == '') || (item.enterWorkDate == '') || (item.currentCity == '') || (item.areaOfHukou == '') || (item.educational == '') || (item.telephone == '') || (item.mailAddress == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})